var mongoose = require('mongoose');
var Comment = require('../models/comment.model');

// 新增
exports.create = function(req,res,next){
    var comment = new Comment(req.body);

    comment.save()
    .then(data=>{
        res.json(data)
    })
    
}

//修改
exports.update = function(req,res,next){
  
    var id = req.params.id; // 取Id
    Comment.findByIdAndUpdate(id,{$set: req.body}, {new:false} )
    .then(data=>{
        res.json(data); // data返回的是你之前修改的那个数据内容
    })
}

//删除
exports.remove = function(req,res,next){
    var id = req.params.id;
    Comment.findByIdAndRemove(id,function(err,doc){
        res.json({msg:'数据删除成功',status:200})
    })
} 


// exports.list = function(req,res,next){
//     var page = (req.body.page) ? parseInt(req.body.page) : 1;
//     var rows = (req.body.rows) ? parseInt(req.body.rows) : 2;
//     var queryCondition = {};
//     var name;
//     if( req.body.name && req.body.name.trim().length>0 ){
//         name = req.body.name;
//         queryCondition = {"name":new RegExp(name,'i')}
//     }


//     Comment.paginate(queryCondition, { page: parseInt(page), rows: parseInt(rows) }, function(err, result) {
//         result.rows = result.docs;
//         delete result.docs;

//        res.json(result)
//     });
// }
exports.list = function(req,res,next){
    var page = (req.body.page) ? parseInt(req.body.page) : 1;
    var rows = (req.body.rows) ? parseInt(req.body.rows) : 2;
    var queryCondition = {};
    var name;
    if( req.body.name && req.body.name.trim().length>0 ){
        name = req.body.name;
        queryCondition = {"name":new RegExp(name,'i')}
    }


    Comment.paginate(queryCondition, { page: parseInt(page), rows: parseInt(rows) }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;

       res.json(result)
    });
}